const now = Date.now || function() {
	return new Date().getTime();
};
const isArray = Array.isArray || function(obj) {
	return obj instanceof Array;
};
const getpv = function(name, arr) {
	let ret = []
	for (let i = 0, len = arr.length; i < len; i++) {
		ret.push(arr[i][name])
	}
	return ret
};
const check_mobile = function(tel) { //手机账号格式判断
	let reg = /^1[0-9]{10}$/;
	if (reg.test(tel)) {
		return true;
	} else {
		return false;
	};
}
//密码
const check_password = function(password) {
	if (password == '' || password == undefined) {
		return false;
	} else if (password.length < 6) {
		return false;
	}
	return true;
}

const format_time = function(inputTime,str='-') {
	//时间戳 转 YY-mm-dd HH:ii:ss 
	var date = new Date(inputTime * 1000);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	var minute = date.getMinutes();
	var second = date.getSeconds();
	minute = minute < 10 ? ('0' + minute) : minute;
	second = second < 10 ? ('0' + second) : second;
	
	return y + str + m + str + d + ' ' + h + ':' + minute + ':' + second;
}

const formatRFC3339 = function(str){
  
  //var str = "2020-08-14T11:33:26+08:00";
  var date = new Date(str).toJSON();
  var newDate=new Date(+new Date(date)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
  return newDate;
  
}

const format_date = function(inputTime) {
	//时间戳 转 YY-mm-dd HH:ii:ss 
	var date = new Date(inputTime * 1000);
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	var minute = date.getMinutes();
	var second = date.getSeconds();
	minute = minute < 10 ? ('0' + minute) : minute;
	second = second < 10 ? ('0' + second) : second;
	return y + '-' + m + '-' + d;
}



const trim = function(testStr) {
	let resultStr = testStr.replace(/\ +/g, ''); //去掉空格
	resultStr = testStr.replace(/[ ]/g, ''); //去掉空格
	resultStr = testStr.replace(/[\r\n]/g, ''); //去掉回车换行
	return resultStr;
}

const check_email = function(email) {
	let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
	if (email == '' || email == undefined) {
		return false;
	} else if (!reg.test(email)) {
		return false;
	} else {
		return true;
	}
}

const price_filter = function(price){
	   price = Number(price).toFixed(2) - 0;
	  return price;
}

const formatLocation = function(res) {
	var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;
	var REGION_PROVINCE = [];
	var addressBean = {
		REGION_PROVINCE: null,
		REGION_COUNTRY: null,
		REGION_CITY: null,
		ADDRESS: null
	};

	function regexAddressBean(address, addressBean) {
		// regex = /^(.*?[市州]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
		regex = /^(.*?[市]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;
		var addxress = regex.exec(address);
		addressBean.REGION_CITY = addxress[1];
		addressBean.REGION_COUNTRY = addxress[2];
		addressBean.ADDRESS = addxress[3] + "(" + res.name + ")";
		// console.log(addxress);
	}
	if (!(REGION_PROVINCE = regex.exec(res.address))) {
		regex = /^(.*?(省|自治区))(.*?)$/;
		REGION_PROVINCE = regex.exec(res.address);
		addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
		regexAddressBean(REGION_PROVINCE[3], addressBean);
	} else {
		addressBean.REGION_PROVINCE = REGION_PROVINCE[1];
		regexAddressBean(res.address, addressBean);
	}
	return addressBean
}

const Timer = {
  data:{},
  start:function(){
    Timer.data = new Date().getTime();
  },
  stop:function(key){
    var time=Timer.data;
    if(time)
    Timer.data = new Date().getTime()-time;
  },
  getTime:function(){
    return Timer.data;
  },
  getLeftTime:function(set_time){
	  let left_time = set_time - Timer.data;
	  if(left_time < 0) {
	  	left_time = 100;
	  }
	  return left_time;
  }
};

export default {
	Timer,
  formatRFC3339,
	getpv,
	now,
	trim,
	check_mobile,
	check_password,
	check_email,
	format_time,
	format_date,
	price_filter,
	formatLocation,
	isArray
}
